/*
 * Created on Apr 9, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.app;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.scohen.juploadr.app.geo.GeoLocation;
import org.scohen.juploadr.app.scalers.ImageScalerFactory;
import org.scohen.juploadr.app.scalers.Scaler;
import org.scohen.juploadr.app.tags.Tag;
import org.scohen.juploadr.prefs.Configuration;
import org.scohen.juploadr.ui.ReusableUIFactory;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RescaledImageAttributes implements UploadImage {
    private boolean rescaled = false;
    private File scaledFile;
    private UploadImage photo;

    private Configuration config = Configuration.getInstance();

    public RescaledImageAttributes(UploadImage source) {
        photo = source;
        rescale();
    }

    public String getDescription() {
        return photo.getDescription();
    }

    public String getImagePath() {
        if (scaledFile != null && scaledFile.exists() && scaledFile.length() > 0) {
            return scaledFile.getAbsolutePath();
        }

        return photo.getImagePath();
    }

    public String getKey() {
        return photo.getKey();
    }

    public int getState() {
        return photo.getState();
    }

    public Set<Tag> getTags() {
        return photo.getTags();
    }

    public String getTitle() {
        return photo.getTitle();
    }

    public boolean hasDescription() {
        return photo.hasDescription();
    }

    public int hashCode() {
        return photo.hashCode();
    }

    public boolean hasTags() {
        return photo.hasTags();
    }

    public boolean hasTitle() {
        return photo.hasTitle();
    }

    public boolean isFamilyViewable() {
        return photo.isFamilyViewable();
    }

    public boolean isFriendViewable() {
        return photo.isFriendViewable();
    }

    public boolean isPubliclyVisible() {
        return photo.isPubliclyVisible();
    }

    public void setFamilyViewable(boolean familyViewable) {
        photo.setFamilyViewable(familyViewable);
    }

    public void setFriendViewable(boolean friendViewable) {
        photo.setFriendViewable(friendViewable);
    }

    public void setImagePath(String imagePath) {
        photo.setImagePath(imagePath);
    }

    public void setPubliclyVisible(boolean publiclyVisible) {
        photo.setPubliclyVisible(publiclyVisible);
    }

    public void setState(int state) {
        photo.setState(state);
    }

    public void setTags(Set<Tag> tags) {
        photo.setTags(tags);
    }

    public void setTitle(String title) {
        photo.setTitle(title);
    }

    public boolean isRescaled() {
        return rescaled;
    }

    public GeoLocation getLocation() {
        return photo.getLocation();
    }

    public void setLocation(GeoLocation loc) {
        photo.setLocation(loc);
    }

    /**
     * rescales the image if necessary
     */
    public void rescale() {
        String imagePath = photo.getImagePath();

        if (config.getAutoResizePhotos()) {

            File origFile = new File(photo.getImagePath());
            Image orig = new Image(ReusableUIFactory.getInstance().getDisplay(), origFile.getAbsolutePath());
            ImageData imageData = orig.getImageData();

            try {

                scaledFile = File.createTempFile("img", imagePath //$NON-NLS-1$
                        .substring(imagePath.lastIndexOf("."))); //$NON-NLS-1$
                imagePath = scaledFile.getAbsolutePath();
                if (Math.max(imageData.width, imageData.height) > config.getMaxPhotoSize()) {
                    Point newSize = getScaledDimensions(imageData);
                    Scaler s = ImageScalerFactory.getScaler();
                    s.scale(origFile, scaledFile, newSize);
                    orig.dispose();
                    this.rescaled = true;
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
    
    private Point getScaledDimensions(ImageData orig) {
        int newWidth;
        int newHeight;

        if (orig.width > orig.height) {
            newWidth = config.getMaxPhotoSize();

            newHeight = (int) Math
                    .floor(((float) orig.height * (float) config.getMaxPhotoSize() / orig.width));
        } else {
            newHeight = config.getMaxPhotoSize();
            newWidth = (int) Math
                    .floor(((float) orig.width * (float) config.getMaxPhotoSize() / orig.height));
        }

        return new Point(newWidth, newHeight);
    }

    public int getRank() {
        return photo.getRank();
    }

    public int getRotation() {
        return photo.getRotation();
    }

    public void setRotation(int rotation) {
        photo.setRotation(rotation);
    }

    public String getPhotoId() {
        return photo.getPhotoId();
    }

}